package com.xuerhua.yxt.controller.sys.fruit;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.StudentFruits;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.FruitsQueryVO;
import com.xuerhua.yxt.service.IStudentFruitsService;
import com.xuerhua.yxt.service.IStudentService;
import com.xuerhua.yxt.utils.CommonUtil;

@RequestMapping("sys/studentFruit/reg")
@Controller
public class SysStudentFruitRegController extends BaseController {
	
	@Autowired
	private IStudentFruitsService studentFruitsService;
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/fruits/studentFruitReg";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(FruitsQueryVO vo){
		try {
			Page<StudentFruits> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<StudentFruits> list = studentFruitsService.selectListByPage(page);
			Grid grid = new Grid();
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("addUpdate")
	@ResponseBody Object addUpdate(StudentFruits record) {
		try {
			if(record.getStudentId() != null) {
				StudentInfo student = studentService.selectById(record.getStudentId());
				record.setStudentName(student.getName());
				record.setGradeName(CommonUtil.getGradeName(student.getGrade()));
			}
			if(record.getId() == null) {
				studentFruitsService.insertSelective(record);
			}else {
				studentFruitsService.updateByPrimaryKeySelective(record);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
}
